#version 450 compatibility







layout(quads, equal_spacing, ccw) in;


in vec4 Normal_ps[];
in vec2 TCoord_ps[];
//in vec4 Position_ps[];

#ifdef DEF_Geometry
out vec4 tsPosition;
#endif


/******************************输出到下一阶段*************************
*
*
*/

#ifdef DEF_Geometry
out vec4 ts_Normal;
out vec2 ts_TCoord;
#else
out vec4 Normal;
out vec2 TCoord;
#endif

/*
*
*
*
********************************************************************/







vec3 interpolate3D(vec3 v0, vec3 v1, vec3 v2) {
    return vec3(gl_TessCoord.x) * v0 + vec3(gl_TessCoord.y) * v1 + vec3(gl_TessCoord.z) * v2;
} 
/*vec3 interpolate4D(vec3 v0, vec3 v1, vec3 v2, vec3 v3) {
    return vec3(gl_TessCoord.x) * v0 + vec3(gl_TessCoord.y) * v1 + vec3(gl_TessCoord.z) * v2 + vec3(gl_TessCoord.z) * v3;
}*/

void main(){
    float u = gl_TessCoord.x;
    float omu = 1-u;
    float v = gl_TessCoord.y;
    float omv = 1-v;

    vec4 vertex = omu*omv * gl_in[0].gl_Position  
                    +  u*omv * gl_in[1].gl_Position  
                    +  u*v*gl_in[2].gl_Position  
                    +  omu*v*gl_in[3].gl_Position;

    
    
    //Normal.xyz = interpolate3D(Normal_ps[0].xyz, Normal_ps[1].xyz, Normal_ps[2].xyz);
#ifdef DEF_Geometry
    ts_Normal.xyz = omu*omv*Normal_ps[0].xyz + u*omv*Normal_ps[1].xyz + u*v*Normal_ps[2].xyz + omu*v*Normal_ps[3].xyz;
    ts_TCoord = (omu*omv*TCoord_ps[0]) + (u*omv*TCoord_ps[1]) + (u*v*TCoord_ps[2]) + (omu*v*TCoord_ps[3]);

    vertex.xyz += normalize(Normal.xyz) * Normal.w;
    vertex.w = 1.0;
    gl_Position = vertex;
#else
    TCoord = (omu*omv*TCoord_ps[0]) + (u*omv*TCoord_ps[1]) + (u*v*TCoord_ps[2]) + (omu*v*TCoord_ps[3]);
    Normal.xyz = omu*omv*Normal_ps[0].xyz + u*omv*Normal_ps[1].xyz + u*v*Normal_ps[2].xyz + omu*v*Normal_ps[3].xyz;

    vertex.xyz += normalize(Normal.xyz) * Normal.w;
    vertex.w = 1.0;
    gl_Position = gl_ProjectionMatrix * vertex;
#endif



    //tsPosition = vertex;
    //TCoord = gl_TessCoord.xy;
}



